package com.ccapton.dragviewdemo.tag;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ccapton.dragviewdemo.R;


/**
 * Created by wenshi on 2019/3/12.
 * Description
 */
public class RandomDragTagView extends LinearLayout implements View.OnTouchListener {

    private String TAG = "RandomDragTagView";
    private String tagType = TagModel.TEXT_TYPE;
    private static final int LEFT = 10001;
    private static final int RIGHT = 10002;
    private static final int CENTER = 10003;
    private static final int TOP = 10004;
    private static final int BOTTOM = 10005;
    private int offset = 40;
    private int dragDirection;
    // 是否显示左侧视图  默认显示左侧视图
    private boolean mIsShowLeftView = true;

    // 呼吸灯动画
    private ValueAnimator mBreathingAnimator;
    // 回弹动画
    private ValueAnimator mReboundAnimator;
    private float mStartReboundX;
    private float mStartReboundY;
    private float mLastMotionRawY;
    private float mLastMotionRawX;

    // 是否多跟手指按下
    private boolean mPointerDown = false;
    private int mTouchSlop = -1;

    // 是否可以拖拽
    private boolean mCanDrag = true;

    // 是否可以拖拽出父控件区域
    private boolean mDragOutParent = false;

    // 父控件最大的高度
    private float mMaxParentHeight = 0;
    // 父控件最大的高度
    private float mMaxParentWidth = 0;

    // 最大挤压宽度 默认400
    private int mMaxExtrusionWidth = 400;
    // 文本圆角矩形的最大宽度
    private float mMaxTextLayoutWidth = 0;
    private float mMaxTextLayoutHeight = 0;

    // 删除标签区域的高度
    private int mDeleteRegionHeight;

    // 暴露接口
    private boolean mStartDrag = false;
    private OnRandomDragListener mDragListener;
    private LinearLayout llContent, llContentVertical;
    private TextView tvContent;
    private EditText etContent;
    private View viewLine, viewLineVertical, rlContent;
    private ImageView ivTop, ivButton, ivLeft, ivRight;

    public static final int COLOR_BLACK = Color.parseColor("#ff000000");
    public static final int COLOR_RED = Color.parseColor("#ffff4444");
    public static final int COLOR_GREEN = Color.parseColor("#ff99cc00");
    public static final int COLOR_ORANGE = Color.parseColor("#ffffbb33");
    public static final int COLOR_BLUE = Color.parseColor("#ff33b5e5");

    private int color = COLOR_BLACK;
    private View viewVertical;
    private View viewHorizontal;

    private float size = 14;

    public RandomDragTagView(Context context) {
        this(context, null);
    }

    public RandomDragTagView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RandomDragTagView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        inflate(context, R.layout.drag_tag_layout, this);
        setOnTouchListener(this);
        initView();
        initListener();
        initData();
    }

    private void initData() {
        // 默认显示左侧视图 隐藏右侧视图
        getMaxTextLayoutWidth();
        // 删除区域的高度
        mDeleteRegionHeight = dip2px(getContext(), 60);
    }

    // 获取文本最大宽度
    public void getMaxTextLayoutWidth() {
        post(new Runnable() {
            @Override
            public void run() {
                mMaxTextLayoutWidth = isShowLeftView() ? llContent.getWidth() : llContent.getWidth();
                mMaxTextLayoutHeight = isShowLeftView() ? llContentVertical.getHeight() : llContentVertical.getHeight();
            }
        });
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    public void refreshSelectStatus() {
        if (isSelected()) {
            if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
                ivTop.setVisibility(View.VISIBLE);
                ivButton.setVisibility(View.VISIBLE);
            } else {
                ivLeft.setVisibility(View.VISIBLE);
                ivRight.setVisibility(View.VISIBLE);
            }
        } else {
            if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
                ivTop.setVisibility(View.INVISIBLE);
                ivButton.setVisibility(View.INVISIBLE);
            } else {
                ivLeft.setVisibility(View.INVISIBLE);
                ivRight.setVisibility(View.INVISIBLE);
            }
        }
    }

    private void initListener() {

    }

    private void initView() {
        etContent = findViewById(R.id.et_content);
        rlContent = findViewById(R.id.rl_text_content);
        llContent = findViewById(R.id.dragView_content);
        llContentVertical = findViewById(R.id.dragView_content_vertical);
        viewLineVertical = findViewById(R.id.view_line_vertical);
        tvContent = findViewById(R.id.tv_content);
        viewLine = findViewById(R.id.view_line);
        ivTop = findViewById(R.id.iv_top);
        ivButton = findViewById(R.id.iv_button);
        ivLeft = findViewById(R.id.iv_left);
        ivRight = findViewById(R.id.iv_right);

        viewVertical = findViewById(R.id.view_vertical);
        viewHorizontal = findViewById(R.id.view_horizontal);

        if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            ivTop.setVisibility(View.VISIBLE);
            ivButton.setVisibility(View.VISIBLE);
        } else {
            ivLeft.setVisibility(View.VISIBLE);
            ivRight.setVisibility(View.VISIBLE);
        }
        initColor();
        initSize();
    }

    private void initSize() {

        if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            ViewGroup.LayoutParams layoutParams = viewVertical.getLayoutParams();
            layoutParams.width = (int) size;
            viewVertical.setLayoutParams(layoutParams);
        } else if (tagType.equals(TagModel.LINE_TYPE)) {
            ViewGroup.LayoutParams layoutParams = viewHorizontal.getLayoutParams();
            layoutParams.height = (int) size;
            viewHorizontal.setLayoutParams(layoutParams);
        } else if (tagType.equals(TagModel.TEXT_TYPE)) {
            tvContent.setTextSize(size);
        }
    }

    public void initColor() {
        if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            viewVertical.setBackgroundColor(color);
        } else if (tagType.equals(TagModel.LINE_TYPE)) {
            viewHorizontal.setBackgroundColor(color);
        } else if (tagType.equals(TagModel.TEXT_TYPE)) {
            tvContent.setTextColor(color);
        }
    }

    public void setColor(int color) {
        this.color = color;
        initColor();
    }

    /**
     * @param newTranslationX
     * @param newTranslationY
     */
    private void checkBound(float newTranslationX, float newTranslationY) {
        setTranslationX(newTranslationX);

        // 越界的情况下 改变textLayout 的高度
        final int parentWidth = ((View) getParent()).getWidth();
        final int parentHeight = ((View) getParent()).getHeight();
        float translationX = getTranslationX();
        float translationY = getTranslationY();
        if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            if (translationY <= 0) {
            } else if (getTranslationY() >= (parentHeight - getHeight())) {
                // 越界检测
                post(new Runnable() {
                    @Override
                    public void run() {
                        if (getTranslationY() >= (parentHeight - getHeight())) {
                            setTranslationY(parentHeight - getHeight());
                        }
                    }
                });
            }

            // 越界检测
            if (getTranslationY() <= 0) {
                setTranslationY(0);
            }

            if (newTranslationX <= 0) {
                newTranslationX = 0;
            } else if (newTranslationX >= parentWidth - getWidth()) {
                newTranslationX = parentWidth - getWidth();
            }

            setTranslationX(newTranslationX);
        } else {
            if (translationX <= 0) {
            } else if (getTranslationX() >= (parentWidth - getWidth())) {
                // 越界检测
                post(new Runnable() {
                    @Override
                    public void run() {
                        if (getTranslationX() >= (parentWidth - getWidth())) {
                            setTranslationX(parentWidth - getWidth());
                        }
                    }
                });
            }

            // 越界检测
            if (getTranslationX() <= 0) {
                setTranslationX(0);
            }

            if (newTranslationY <= 0) {
                newTranslationY = 0;
            } else if (newTranslationY >= parentHeight - getHeight()) {
                newTranslationY = parentHeight - getHeight();
            }

            setTranslationY(newTranslationY);
        }
    }

    /**
     * 添加标签
     *
     * @param text           标签文本
     * @param translationX   相对于父控件的x坐标
     * @param translationY   相对于父控件的y坐标
     * @param isShowLeftView 是否显示左侧标签
     */
    public void initTagView(String text, String tagType,int width,int height, int color, int size, final float translationX, final float translationY, boolean isShowLeftView) {
        this.mIsShowLeftView = isShowLeftView;
        this.tagType = tagType;
        this.color = color;
        this.size = size;
        // 不可见
        setVisibility(INVISIBLE);
        if (tagType.equals(TagModel.TEXT_TYPE)) {
            llContent.setVisibility(View.VISIBLE);
            llContentVertical.setVisibility(View.GONE);
            rlContent.setVisibility(View.VISIBLE);
            viewLine.setVisibility(View.GONE);
            // 设置文本控件
            if (mIsShowLeftView) {
                tvContent.setText(text);
            } else {
                tvContent.setText(text);
            }
            if(width!=0&&height!=0){
                ViewGroup.LayoutParams layoutParams = llContent.getLayoutParams();
                layoutParams.width = width;
                llContent.setLayoutParams(layoutParams);
            }
        } else if (tagType.equals(TagModel.LINE_TYPE)) {
            llContentVertical.setVisibility(View.GONE);
            llContent.setVisibility(View.VISIBLE);
            rlContent.setVisibility(View.GONE);
            viewLine.setVisibility(View.VISIBLE);
            if(width!=0&&height!=0){
                ViewGroup.LayoutParams layoutParams = llContent.getLayoutParams();
                layoutParams.width = width;
                llContent.setLayoutParams(layoutParams);
            }
        } else if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            llContentVertical.setVisibility(View.VISIBLE);
            llContent.setVisibility(View.GONE);
            if(width!=0&&height!=0){
                ViewGroup.LayoutParams layoutParams = llContentVertical.getLayoutParams();
                layoutParams.height = height;
                llContentVertical.setLayoutParams(layoutParams);
            }
        }


        initColor();
        initSize();
        // 获取文本最大宽度
        getMaxTextLayoutWidth();

        post(new Runnable() {
            @Override
            public void run() {
                // 边界检测
                checkBound(translationX, translationY);

                // 设置可见
                setVisibility(VISIBLE);
            }
        });
    }
    boolean isMove = false;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mTouchSlop < 0) {
            mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        }
        if (!canDragView()) return super.onTouchEvent(event);
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                isMove = false;
                final float x = event.getRawX();
                final float y = event.getRawY();
                getParent().requestDisallowInterceptTouchEvent(true);
                mStartDrag = false;
                mPointerDown = false;
                mLastMotionRawX = x;
                mLastMotionRawY = y;
                mStartReboundX = getTranslationX();
                mStartReboundY = getTranslationY();
                if (mDragListener != null) {
                    mDragListener.onSelectTouch(etContent);
                }
                // 调整索引 位于其他标签之上
                adjustIndex();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                mPointerDown = true;
                break;
            case MotionEvent.ACTION_MOVE:
                final float rawY = event.getRawY();
                final float rawX = event.getRawX();
                if (!mStartDrag) {
                    mStartDrag = true;
                    if (mDragListener != null) {
                        mDragListener.onStartDrag();
                    }
                }
                if (!mPointerDown) {
                    final float yDiff = rawY - mLastMotionRawY;
                    final float xDiff = rawX - mLastMotionRawX;
                    if(Math.abs(xDiff)>0 || Math.abs(yDiff)>0){
                        isMove = true;
                    }
                    // 处理move事件
                    handlerMoveEvent(yDiff, xDiff);
                    mLastMotionRawY = rawY;
                    mLastMotionRawX = rawX;
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                mPointerDown = false;
                break;
            // case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mPointerDown = false;
                mStartDrag = false;

                if(!isMove){
                    if (mDragListener != null) {
                        etContent.setText(tvContent.getText().toString());
                        mDragListener.onClick(isSelected(),RandomDragTagView.this,etContent);
                    }

                }

                getParent().requestDisallowInterceptTouchEvent(false);
                if (mDragListener != null) {
                    mDragListener.onStopDrag();
                }

                break;
        }
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();
            dragDirection = getDirection(v, (int) event.getX(), (int) event.getY());
        }

        if (isSelected()) {
            if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
                if (dragDirection == TOP || dragDirection == BOTTOM) {
                    if (action == MotionEvent.ACTION_DOWN) {
                        height = getHeight();
                        int parentHeight = ((View) getParent()).getHeight();
                        rightHeight = parentHeight - getBottom() - getTranslationY();
                    }
                    handleDrag(v, event, action);
                    invalidate();
                    return true;
                }
            } else {
                if (dragDirection == LEFT || dragDirection == RIGHT) {
                    if (action == MotionEvent.ACTION_DOWN) {
                        width = getWidth();
                        int parentWidth = ((View) getParent()).getWidth();
                        rightWidth = parentWidth - getRight() - getTranslationX();
                    }
                    handleDrag(v, event, action);
                    invalidate();
                    return true;
                }
            }
        }


        return false;
    }

    private float lastX, lastY;
    private float width, height;
    private float rightWidth, rightHeight;

    private void handleDrag(View v, MotionEvent event, int action) {
        switch (action) {
            case MotionEvent.ACTION_UP:
                dragDirection = 0;
                break;
            case MotionEvent.ACTION_MOVE:
                int parentWidth = ((View) getParent()).getWidth();
                int parentHeight = ((View) getParent()).getHeight();
                float dx = event.getRawX() - lastX;
                float dy = event.getRawY() - lastY;

                switch (dragDirection) {
                    case LEFT: // 左边缘
                        if (parentWidth - getWidth() - rightWidth <= 0 && dx < 0) {
                            return;
                        }
                        left(v, dx);
                        break;
                    case RIGHT:
                        if (parentWidth - getWidth() - getTranslationX() <= 0 && dx > 0) {
                            return;
                        }
                        right(v, dx);
                        break;
                    case TOP:
                        if (parentHeight - getHeight() - rightHeight <= 0 && dy < 0) {
                            return;
                        }
                        top(v, dy);
                        break;
                    case BOTTOM:
                        if (parentHeight - getHeight() - getTranslationY() <= 0 && dy > 0) {
                            return;
                        }
                        botton(v, dy);
                        break;
                    default:
                        break;
                }
                lastX = event.getRawX();
                lastY = event.getRawY();
                break;
            default:
                break;
        }
    }

    private void botton(View v, float dx) {
        height += dx;
        if (height < 120) {
            height = 120;
        }
        float translationY = getTranslationY();
        if (llContentVertical != null) {
            ViewGroup.LayoutParams layoutParams = llContentVertical.getLayoutParams();
            layoutParams.height = (int) height;
            llContentVertical.setLayoutParams(layoutParams);
        }
        refreshMaxWidthHeight();
    }

    private void top(View v, float dx) {
        height -= dx;
        if (height < 120) {
            height = 120;
        }
        float translationX = getTranslationX();
        if (llContentVertical != null) {
            ViewGroup.LayoutParams layoutParams = llContentVertical.getLayoutParams();
            layoutParams.height = (int) height;
            llContentVertical.setLayoutParams(layoutParams);
        }
        refreshMaxWidthHeight();
        handleHeightError(dx);
    }

    private void right(View v, float dx) {
        width += dx;
        if (width < 120) {
            width = 120;
        }
        float translationY = getTranslationY();
        if (llContent != null) {
            ViewGroup.LayoutParams layoutParams = llContent.getLayoutParams();
            layoutParams.width = (int) width;
            llContent.setLayoutParams(layoutParams);
        }
        refreshMaxWidthHeight();
        // 高度越界处理
        if (translationY <= 0) {
            translationY = 0;
        } else if (translationY >= mMaxParentHeight) {
            translationY = mMaxParentHeight;
        }
        setTranslationY(translationY);

    }


    private void left(View v, float dx) {
        width -= dx;
        if (width < 120) {
            width = 120;
        }
        float translationY = getTranslationY();
        if (llContent != null) {
            ViewGroup.LayoutParams layoutParams = llContent.getLayoutParams();
            layoutParams.width = (int) width;
            llContent.setLayoutParams(layoutParams);
        }
        refreshMaxWidthHeight();
        handleWidthLeftError(dx);
        // 高度越界处理
        if (translationY <= 0) {
            translationY = 0;
        } else if (translationY >= mMaxParentHeight) {
            translationY = mMaxParentHeight;
        }
        setTranslationY(translationY);

    }

    private void handleHeightError(final float dx) {
        post(new Runnable() {
            @Override
            public void run() {
                if (getParent() == null) return;
                int parentHeight = ((View) getParent()).getHeight();
                float maxWidth = parentHeight - llContentVertical.getHeight() - rightHeight;
                setTranslationY(maxWidth);
            }
        });
    }

    private void handleWidthLeftError(final float dx) {
        post(new Runnable() {
            @Override
            public void run() {
                if (getParent() == null) return;
                int parentWidth = ((View) getParent()).getWidth();
                float maxWidth = parentWidth - llContent.getWidth() - rightWidth;
                setTranslationX(maxWidth);
            }
        });
    }

    public void refreshMaxWidthHeight() {

        getMaxTextLayoutWidth();
        int parentHeight = ((View) getParent()).getHeight();
        int parentWidth = ((View) getParent()).getWidth();
        int parentParentHeight = ((View) getParent().getParent()).getHeight();
        int parentParentWidth = ((View) getParent().getParent()).getWidth();
        mMaxParentHeight = (mDragOutParent ? parentParentHeight : parentHeight) - getHeight();
        mMaxParentWidth = (mDragOutParent ? parentParentWidth : parentWidth) - getWidth();
        ;
    }

    /**
     * 通过坐标和移动距离，确定移动方向
     *
     * @param v
     * @param x
     * @param y
     * @return
     */
    private int getDirection(View v, int x, int y) {
        int left = v.getLeft();
        int right = v.getRight();
        int bottom = v.getBottom();
        int top = v.getTop();
        if (tagType.equals(TagModel.LINE_TYPE_VERTICAL)) {
            if (y < offset) {
                return TOP;
            }

            if ((bottom - top) - y < offset) {
                return BOTTOM;
            }
        } else {
            if (x < offset) {
                return LEFT;
            }
            if ((right - left) - x < offset) {
                return RIGHT;
            }
        }


        return CENTER;
    }

    /**
     * 处理手势的move事件
     *
     * @param yDiff y轴方向的偏移量
     * @param xDiff x轴方向的偏移量
     */
    private void handlerMoveEvent(float yDiff, float xDiff) {
        float translationX = getTranslationX() + xDiff;
        float translationY = getTranslationY() + yDiff;

        // 越界处理 最大最小原则
        int parentWidth = ((View) getParent()).getWidth();
        int parentHeight = ((View) getParent()).getHeight();
        if (mMaxParentHeight == 0) {
            int parentParentHeight = ((View) getParent().getParent()).getHeight();
            mMaxParentHeight = (mDragOutParent ? parentParentHeight : parentHeight) - getHeight();
        }
        int maxWidth = parentWidth - getWidth();

        // 分情况处理越界 宽度
        if (translationX <= 0) {
            translationX = 0;
        } else if (translationX >= maxWidth) {
            translationX = maxWidth;
            handleWidthError();
        } else {
            int textWidth = isShowLeftView() ? llContent.getWidth() : llContent.getWidth();
            // 左侧视图
            if (isShowLeftView()) {
                if (getTranslationX() == 0 && textWidth < mMaxTextLayoutWidth) {
                    translationX = 0;
                }
            } else {
                if (textWidth < mMaxTextLayoutWidth) {
                    handleWidthError();
                }
            }
        }
        // 高度越界处理
        if (translationY <= 0) {
            translationY = 0;
        } else if (translationY >= mMaxParentHeight) {
            translationY = mMaxParentHeight;
        }
        rightWidth = parentWidth - getRight() - getTranslationX();
        setTranslationX(translationX);
        setTranslationY(translationY);
    }

    // 处理宽度误差
    private void handleWidthError() {
        post(new Runnable() {
            @Override
            public void run() {
                if (getParent() == null) return;
                int parentWidth = ((View) getParent()).getWidth();
                int maxWidth = parentWidth - getWidth();
                setTranslationX(maxWidth);
            }
        });
    }

    /**
     * 调整索引 位于其他标签之上
     * moveToTop(View target) 方法的性能更好
     */
    @Deprecated
    private void adjustIndex() {
        ViewParent parent = getParent();
        if (parent != null) {
            if (parent instanceof ViewGroup) {
                ViewGroup parentView = (ViewGroup) parent;
                int childCount = parentView.getChildCount();
                if (childCount > 1 && indexOfChild(this) != (childCount - 1)) {
                    parentView.removeView(this);
                    parentView.addView(this);
                }
            }
        }
    }

    private void moveToTop(View target) {
        //先确定现在在哪个位置
        int startIndex = indexOfChild(target);
        //计算一共需要几次交换，就可到达最上面
        int count = getChildCount() - 1 - startIndex;
        for (int i = 0; i < count; i++) {
            //更新索引
            int fromIndex = indexOfChild(target);
            //目标是它的上层
            int toIndex = fromIndex + 1;
            //获取需要交换位置的两个子View
            View from = target;
            View to = getChildAt(toIndex);

            //先把它们拿出来
            detachViewFromParent(toIndex);
            detachViewFromParent(fromIndex);

            //再放回去，但是放回去的位置(索引)互换了
            attachViewToParent(to, fromIndex, to.getLayoutParams());
            attachViewToParent(from, toIndex, from.getLayoutParams());
        }
        //刷新
        invalidate();
    }

    // 移除标签view
    public void removeTagView() {
        ViewParent parent = getParent();
        if (parent != null) {
            if (parent instanceof ViewGroup) {
                ViewGroup parentView = (ViewGroup) parent;
                parentView.removeView(this);
            }
        }
    }

    // 是否可以拖拽
    public boolean canDragView() {
        return mCanDrag;
    }

    public boolean isShowLeftView() {
        return mIsShowLeftView;
    }

    public String getTagText() {
        if (isShowLeftView()) {
            return tvContent.getText().toString();
        }
        return tvContent.getText().toString();
    }

    public void setTagText(String text){
        tvContent.setText(text);
    }

    public float getPercentTransX() {
        int parentWidth = ((View) getParent()).getWidth();
        return getTranslationX() / parentWidth;
    }

    public float getPercentTransY() {
        int parentHeight = ((View) getParent()).getHeight();
        return getTranslationY() / parentHeight;
    }

    public void setShowLeftView(boolean showLeftView) {
        mIsShowLeftView = showLeftView;
    }

    public void setMaxExtrusionWidth(int maxExtrusionWidth) {
        mMaxExtrusionWidth = maxExtrusionWidth;
    }

    public int getMaxExtrusionWidth() {
        return mMaxExtrusionWidth;
    }

    @Override
    protected void onDetachedFromWindow() {
        clearBreathingAnimator();
        super.onDetachedFromWindow();
    }

    // 注意清理 防止内存泄露
    private void clearBreathingAnimator() {
        if (mBreathingAnimator != null && mBreathingAnimator.isRunning()) {
            mBreathingAnimator.cancel();
            mBreathingAnimator = null;
        }
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public int getColor() {
        return color;
    }

    public float getSize() {
        return size;
    }

    public void setSize(float size) {
        this.size = size;
        initSize();
    }

    public interface OnRandomDragListener {
        // 开始拖拽
        void onStartDrag();

        // 停止拖拽
        void onStopDrag();

        void onClick(boolean isSelect,RandomDragTagView tagView,EditText editText);

        void onSelectTouch(EditText editText);
    }

    public String getTagType() {
        return tagType;
    }

    public void setOnRandomDragListener(OnRandomDragListener listener) {
        this.mDragListener = listener;
    }
}
